//
//  ViewController.swift
//  12picketview
//
//  Created by Clover on 2018/4/19.
//  Copyright © 2018年 youbing. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {

    @IBOutlet weak var resultLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!
    var imageArr = [NSString]()
    var dataArr1 = [Int]()
    var dataArr2 = [Int]()
    var dataArr3 = [Int]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imageArr = ["👻","👸","💩","😘","🍔","🤖","🍟","🐼","🚖","🐷"]
        for _ in 0...100 {
            let info = (Int)(arc4random() % 10 )
            self.dataArr1.append(info)
            self.dataArr2.append(info)
            self.dataArr3.append(info)
        }
        
         resultLabel.text = ""
    }

    
    @IBAction func starClick(_ sender: UIButton) {
        pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 0, animated: true)
        pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 1, animated: true)
        pickerView.selectRow(Int(arc4random()) % 90 + 3, inComponent: 2, animated: true)
        
        if(dataArr1[pickerView.selectedRow(inComponent: 0)] == dataArr2[pickerView.selectedRow(inComponent: 1)] && dataArr2[pickerView.selectedRow(inComponent: 1)] == dataArr3[pickerView.selectedRow(inComponent: 2)]) {
            
            resultLabel.text = "Bingo!"
            
        } else {
            
            resultLabel.text = "💔"
            
        }
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 100
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 100.0
    }
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return 100.0
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let pickLbl = UILabel()
        
        if component == 0 {
           
            pickLbl.text = imageArr[dataArr1[row]] as String
        }else if component == 2{
            pickLbl.text = imageArr[dataArr2[row]] as String
        }else{
            pickLbl.text = imageArr[dataArr3[row]] as String
        }
        
        pickLbl.font = UIFont(name: "Apple Color Emoji", size: 80)
        pickLbl.textAlignment = NSTextAlignment.center
        return pickLbl
    }
}

